/**
 * ElectronExchanger Server Header File
 * 
 * File:        db_helper.h
 * Brief:       BRIEF
 * Author:      Shen Fuyuan, Chen Kangbing
 * Email:       EMAIL
 * Modified:    2020/09/13 11:20:54
 *
 */


#ifndef DB_HELPER_H
#define DB_HELPER_H

#ifdef __cplusplus
extern "C" {
#endif 


#include <mysql/mysql.h>


typedef struct {
    int UserID;
    char UserEmail[50];
    char NickName[50];
    char Password[50];
    char Salt[50];
    int RegistrationTime;
} DB_UserDataTypeDef;

typedef struct {
    int EmailNo;
    char FromEmail[50];
    char ToEmails[1000];
    char BCCEmails[1000];
    char Subject[1000];
    int FileID;
    // char FileName[50];
    int Time;
} DB_EmailInfoTypeDef;

typedef struct {
    int EmailNo;
    char Subject[1000];
    int Time;
    int ReadState;
} DB_EmailListTypeDef;

// typedef struct {
//     int UserID;
//     int EmailID;
//     int ReadState;
// } DB_InboxDataTypeDef;

// typedef struct {
//     int UserID;
//     int EmailID;
// } DB_DraftsDataTypeDef;

// typedef struct {
//     int UserID;
//     int EmailID;
// } DB_TrashDataTypeDef;

typedef struct {
    int UserID;
    int ContactID;
    char ContactNickName[50];
} DB_ContactDataTypeDef;

typedef struct {
    int FileID;
    char FileName[255];
    int FileSize;
    char FilePath[1024];
    int FileTime;
} DB_FileDataTypeDef;


extern MYSQL* conn_prt;

extern MYSQL_RES* res;

extern MYSQL_ROW row;

/********************Open Sql********************/

int mysql_OpenSql();


/********************Close Sql********************/

int mysql_CloseSql();


/********************Check Sql********************/

int mysql_check(char* Table, char* name, char* values);


/********************Get Server data********************/

int mysql_get_serverdata(char* name, int* values);


/********************Set Server data********************/

int mysql_set_serverdata(char* name, int data);


/********************Register Sql********************/

int mysql_register(DB_UserDataTypeDef* user);


/********************Create Session ID********************/

int mysql_create_session_id(char* session_id, int user_id, int expire_time);


/********************Destroy Session ID********************/

int mysql_destroy_session_id(char* session_id);


/********************Check Session ID********************/

int mysql_check_session_id(char* session_id, int* user_id, int* expire_time);


/********************Refresh Session ID********************/

int mysql_refresh_session_id(char* session_id, int expire_time);


/********************Set Password********************/

int mysql_set_password(int uid, char *new_password, char *salt);


/********************Get Information********************/

int mysql_get_userinfo(int uid, DB_UserDataTypeDef* user);


/********************Set NickName********************/

int mysql_set_nickname(int uid, char* nickname);


/********************Get File********************/

int mysql_get_file(int fid, DB_FileDataTypeDef* file);


/********************Upload File********************/

int mysql_upload_file(DB_FileDataTypeDef* file);


/********************Get Box Size********************/

int mysql_get_box_size(int uid, char* box, int* list_size);


/********************Set Contact NickName********************/

int mysql_set_contact_nickname(int uid, int cid, char* new_nickname);


/********************Add Contact********************/

int mysql_add_contact(DB_ContactDataTypeDef* contact);


/********************Delete Contact********************/

int mysql_delete_contact(int uid, int cid);


/********************Get Target UserID********************/

int mysql_get_target_userid(char* email, int* target_uid);


/********************Get Target NickName********************/

int mysql_get_target_nickname(int target_uid, char* nickname);


/********************Get Mail List********************/

int mysql_get_mail_list(int uid, char* box, DB_EmailListTypeDef* list, int *list_size);


/********************Get Mail********************/

int mysql_get_mail(int eno, DB_EmailInfoTypeDef* mail, char *content);


/********************Upload Mail********************/

int mysql_upload_mail(DB_EmailInfoTypeDef* mail, char *content);


/********************Save Mail********************/

int mysql_save_mail(int eno, int uid, char* box);


/********************Delete Mail********************/

int mysql_delete_mail(int eno, int uid, char* box);


/********************Get Contact List********************/

int mysql_get_contact_list(int uid, DB_ContactDataTypeDef *contact_list, int *list_size);


/********************Set Inbox Readstate********************/

int mysql_set_read_state(int eno, int uid, int read_state);

/********************Check Mail Exist********************/

int mysql_check_mail_exist(int uid, int eno, char *box, int *exist);


#ifdef __cplusplus
}
#endif 

#endif
